package com.heima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.mapper.SkuClassMapper;
import com.heima.pojo.SkuClass;
import com.heima.pojo.dto.SkuClassDto;
import com.heima.pojo.vo.PageResponse;
import com.heima.service.SkuClassService;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class SkuClassServiceImpl implements SkuClassService {
    
    private final SkuClassMapper skuClassMapper;

    /**
     * 新增商品类型
     *
     * @param skuClassDto
     * @return
     */
    @Override
    public Boolean insertSkuClass(SkuClassDto skuClassDto) {
        SkuClass skuClass = BeanUtil.copyProperties(skuClassDto, SkuClass.class);
        return skuClassMapper.insert(skuClass) == 1;
    }

    /**
     * 商品类型搜索
     *
     * @param pageIndex
     * @param pageSize
     * @param className
     * @return
     */
    @Override
    public PageResponse<SkuClass> selectPage(Integer pageIndex, Integer pageSize, String className) {
        Page<SkuClass> page = new Page<>(pageIndex, pageSize);

        LambdaQueryWrapper<SkuClass> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(ObjectUtil.isNotEmpty(className), SkuClass::getClassName, className);
        skuClassMapper.selectPage(page, wrapper);

        PageResponse<SkuClass> pageResponse = new PageResponse<>();
        pageResponse.setPageIndex(String.valueOf(pageIndex));
        pageResponse.setPageSize(String.valueOf(pageSize));
        pageResponse.setTotalPage(String.valueOf(page.getPages()));
        pageResponse.setTotalCount(String.valueOf(page.getTotal()));
        pageResponse.setCurrentPageRecords(page.getRecords());

        return pageResponse;
    }

    /**
     * 根据id查询商品类型
     *
     * @param classId
     * @return
     */
    @Override
    public SkuClass selectById(String classId) {
        LambdaQueryWrapper<SkuClass> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SkuClass::getClassId, classId);

        return skuClassMapper.selectOne(wrapper);
    }

    /**
     * 根据id修改商品类型信息
     *
     * @param classId
     * @param dto
     * @return
     */
    @Override
    public Boolean updateSkuClass(String classId, SkuClassDto dto) {
        UpdateWrapper<SkuClass> wrapper = new UpdateWrapper<>();
        wrapper.lambda().set(SkuClass::getClassName, dto.getClassName());
        wrapper.lambda().eq(SkuClass::getClassId, classId);

        return skuClassMapper.update(null, wrapper) == 1;
    }

    /**
     * 根据id删除商品类型
     *
     * @param classId
     * @return
     */
    @Override
    public Boolean deleteSkuClass(String classId) {
        LambdaQueryWrapper<SkuClass> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SkuClass::getClassId, classId);

        return skuClassMapper.delete(wrapper) == 1;
    }
}
